//
//  ZNGUserInfoDataSource.m
//  开店通
//
//  Created by 张浩 on 15/7/15.
//  Copyright (c) 2015年 张浩. All rights reserved.
//

#import "ZNGUser.h"
#import "MJExtension.h"

@interface ZNGUser ()
{
    BOOL _online;
    NSString *_userId;
    NSString *_token;
    NSString *_userName;
    NSArray *_county;
    NSArray *_brandList;
    BOOL _hasBind;
}
@end

@implementation ZNGUser

ZNGSingletonM(userInfo)

+ (void)logout
{
    [[self userInfo] logout];
}

+ (void)loginWithUserData:(ZNGUserData *)userData
{
    [[self userInfo] loginWithUserData:userData];
}

- (void)loginWithUserData:(ZNGUserData *)userData
{
    self.online = YES;
    self.userId = userData.object.userid;
    self.token =  userData.token;
    self.userName = userData.object.mobilePhone;
    self.county = userData.county;
    self.brandList = userData.brandAuth.array;
}

- (void)logout
{
    self.online = NO;
    self.userId = @"";
    self.token = @"";
    self.county = nil;
}

- (void)setOnline:(BOOL)online
{
    _online = online;
    [[NSUserDefaults standardUserDefaults] setBool:online forKey:@"online"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (BOOL)isOnline
{
    return [[NSUserDefaults standardUserDefaults] boolForKey:@"online"];
}

- (void)setToken:(NSString *)token
{
    _token = token;
    
    [[NSUserDefaults standardUserDefaults] setValue:token forKey:@"token"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSString *)token
{
    return [[NSUserDefaults standardUserDefaults] valueForKey:@"token"];
}

- (void)setUserId:(NSString *)userId
{
    _userId = userId;
    
    [[NSUserDefaults standardUserDefaults] setValue:userId forKey:@"userId"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSString *)userId
{
    return [[NSUserDefaults standardUserDefaults] valueForKey:@"userId"];
}

- (void)setUserName:(NSString *)userName
{
    _userName = userName;
    
    [[NSUserDefaults standardUserDefaults] setValue:userName forKey:@"userName"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSString *)userName
{
    return [[NSUserDefaults standardUserDefaults] valueForKey:@"userName"];
}

- (void)setCounty:(NSArray *)county
{
    _county = county;
    [[NSUserDefaults standardUserDefaults] setObject:county forKey:@"county"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSArray *)county
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"county"];
}

- (void)setBrandList:(NSArray *)brandList
{
    _brandList = brandList;
    [[NSUserDefaults standardUserDefaults] setObject:brandList forKey:@"brandList"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSArray *)brandList
{
    NSArray *array = [[NSUserDefaults standardUserDefaults] valueForKey:@"brandList"];
    NSArray *brandList = [ZNGBrandInfo mj_objectArrayWithKeyValuesArray:array];

    return brandList;
}

- (NSString *)registrationID
{
    return [[NSUserDefaults standardUserDefaults] valueForKey:@"registrationID"];
}

- (void)setHasBind:(BOOL)hasBind
{
    _hasBind = hasBind;
    [[NSUserDefaults standardUserDefaults] setBool:hasBind forKey:@"hasBind"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (BOOL)hasBind
{
    return [[NSUserDefaults standardUserDefaults] boolForKey:@"hasBind"];
}
@end

@implementation ZNGUserData

@end

@implementation ZNGUserInfo

@end

@implementation ZNGLoginParam

@end

@implementation ZNGLoginResult

@end

@implementation ZNGBrandInfo

@end

@implementation ZNGBrandAuthList

@end